//
//  ShowFirstViewController.m
//  WuliHanhan
//
//  Created by tang on 16/3/9.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "ShowFirstViewController.h"


#import "FirstVCTableViewCell.h"
#import "CustomNavgation.h"
#import "Header.h"
#import "MJRefresh.h"

#import "FirstSearchViewController.h"
#import "ShowDetalImageViewController.h"



#import "WuLiGetShouyeRequest.h"


//#import "FirstViewController.h"

@interface ShowFirstViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate,
                                                                                    btndelgate>
{
    
    CustomNavgation *_customNavView;
    float lastContentOffset;
    NSMutableArray *dataMutableArray;
    NSString *pageStrnig;
    
    
}

@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic,strong)UIView *topView;

@end

@implementation ShowFirstViewController


- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController setNavigationBarHidden:YES];
    
    dataMutableArray = [NSMutableArray new];
    
    _mainTableView = [[UITableView alloc] initWithFrame:
                      CGRectMake(0, 64,WithView,heightView - 64)];
    [self.view addSubview:_mainTableView];
    
    
    self.automaticallyAdjustsScrollViewInsets = false;
    
    _customNavView = [[CustomNavgation alloc] initWithFrame:CGRectMake(0, 0, WithView, 64)];
    [_customNavView setUserInteractionEnabled:YES];
    [self.view addSubview:_customNavView];
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
    [self.view addSubview:_topView];
    
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [_mainTableView setDelegate:self];
    [_mainTableView setDataSource:self];
    [_mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    

    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    
    [WuLiGetShouyeRequest requestMianModelWithPage:@"1"];
        
    }];
    [_mainTableView.mj_header beginRefreshing];
    
    [_customNavView setDelegate:self];
    pageStrnig = @"1";
    [_topView setBackgroundColor:uicolor(253, 202, 47)];
    
    [self addserVer];
    
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    [button setBackgroundColor:[UIColor redColor]];
//    [self.view addSubview:button];
//    [button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];

    
}
- (void)press
{
//    FirstViewController *firstVC = [[FirstViewController alloc] init];
//    [self presentViewController:firstVC animated:YES completion:nil];
}

/*监听数据*/
- (void)addserVer
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouyeModelGetBack:) name:shouyeshuju object:nil];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController setHidesBottomBarWhenPushed:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [self.tabBarController setHidesBottomBarWhenPushed:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark---首页数据返回
- (void)shouyeModelGetBack:(NSNotification *)notion
{
//    NSLog(@"%@", [notion userInfo]);
    
    [_mainTableView.mj_header endRefreshing];
    
    NSDictionary *dic = [notion userInfo];
    
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        NSArray *arrya = [dic valueForKey:@"data"];
        NSMutableArray *mutableArray = [NSMutableArray new];
        
        for (NSDictionary *keyDic in arrya)
        {
            if ([[keyDic valueForKey:@"type"] isEqualToString:@"work"])
            {
                [mutableArray addObject:keyDic];
            }
            else
            {
                
            }
        }
        
        
        if ([pageStrnig isEqualToString:@"1"])
        {
            [dataMutableArray removeAllObjects];
            [dataMutableArray addObjectsFromArray:mutableArray];
        }
        else
        {
            [dataMutableArray addObjectsFromArray:mutableArray];
        }
        [_mainTableView reloadData];
    }
    else
    {
        
    }
}


// CustomDelgate Respond Event

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataMutableArray count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInden = @"cell";
    FirstVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];
    if (!cell) {
        cell = [[FirstVCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden];
    }
    [cell setModel:dataMutableArray[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}




/*点击*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShowDetalImageViewController *detalView = [[ShowDetalImageViewController alloc] init];
    detalView.IDString = [NSString stringWithFormat:@"%@", [[[dataMutableArray objectAtIndex:indexPath.row] valueForKey:@"data"] valueForKey:@"id"]];
    detalView.textStr = [NSString stringWithFormat:@"%@",[[[dataMutableArray objectAtIndex:indexPath.row]valueForKey:@"data"]valueForKey:@"title" ]];
    [self.navigationController pushViewController:detalView animated:YES];
}







- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    lastContentOffset = scrollView.contentOffset.y;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 64) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame;
            frame = _customNavView.frame;
            CGFloat delta = frame.origin.y + 44;
            frame.origin.y = MAX(-100, frame.origin.y - delta);
            _customNavView.frame = frame;
            _customNavView.alpha = 0;
            [_mainTableView setFrame:CGRectMake(0, 20, WithView, heightView-49.0-20)];
        }];
    }
    
    
    if (lastContentOffset - 100 >= scrollView.contentOffset.y) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame;
            frame = _customNavView.frame;
            CGFloat delta = frame.origin.y - 44;
            frame.origin.y = MIN(0, frame.origin.y - delta);
            _customNavView.frame = frame;
            _customNavView.alpha = 1.0;
            [_mainTableView setFrame:CGRectMake(0, 64.0, WithView, heightView-49.0-64.0)];
        }];
    }
    
    if (scrollView.contentOffset.y <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame;
            frame = _customNavView.frame;
            CGFloat delta = frame.origin.y - 44;
            frame.origin.y = MIN(0, frame.origin.y - delta);
            _customNavView.frame = frame;
            _customNavView.alpha = 1.0;
            [_mainTableView setFrame:CGRectMake(0, 64.0, WithView, heightView-49.0-64.0)];
        }];
    }
}





/*--点击搜索--*/
- (void)touchBtn
{
    FirstSearchViewController *firstSearchVC = [[FirstSearchViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstSearchVC];
    [self presentViewController:nav animated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
